<?php

class Application_Model_DbTable_Rules extends Zend_Db_Table_Abstract
{

    protected $_name = 'rules';
    
    protected $_referenceMap = array(
        "groups"=>array(
            'columns' => array('Groups'),
            'refTableClass' => "Application_Model_DbTable_Groups",
            'refColumns' => array("idGroups"),
            'onUpdate' => self::RESTRICT,
            'onDelete' => self::CASCADE
        ),
        "actions"=>array(
            'columns' => array('Pages'),
            'refTableClass' => "Application_Model_DbTable_Actions",
            'refColumns' => array("idActions"),
            'onUpdate' => self::RESTRICT,
            'onDelete' => self::CASCADE
        )
    );
    
    
    public function fetchDataPermission() {
        /**
         *   SELECT groups.GroupsName AS 'group', controllers.ControllersName AS 'controller', actions.ActionsName AS 'action', rules.Stauts AS 'allow'
         *   FROM `rules` , `users` , `groups` , `actions` , `controllers`
         *   WHERE users.Groups = groups.idGroups
         *   AND groups.idGroups = rules.Groups
         *   AND rules.pages = actions.idactions
         *   AND actions.Controllers = controllers.idControllers
        */
        $select = $this->select()
                       ->from(array('r'=>"rules"),array("allow"=>"r.Status"))
                       ->join(array('ac'=>"actions"),"r.Pages=ac.idActions",array('action'=>"ac.ActionsName"))
                       ->join(array('cl'=>"controllers"),"ac.Controllers=cl.idControllers",array('controller'=>"cl.ControllersName"))
                       ->join(array('gr'=>"groups"),"gr.idGroups=r.Groups",array('group'=>"gr.GroupsName"))
                       ->setIntegrityCheck(false);
        
        return $this->fetchAll($select);
        
    }
}

